<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            检测单明细处理
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <div id="citemList" class="aui-content">
            <div class="aui-form" id="citemForm">

            </div>
        </div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    var chkBillDetailsId = "";
    var chkBillCode = "";
    var myFlag = false;

    //初始化
    apiready = function () {

        chkBillDetailsId = api.pageParam.chkBillDetailsId;
        console.log(chkBillDetailsId)
        chkBillCode = api.pageParam.chkBillCode;
        getCitemsByDetailId(chkBillDetailsId);
    };

    var list = [];

    function getCitemsByDetailId() {
        api.showProgress({
            title: '加载中...',
        });

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getCitemsByDetailId";
        var data = {
            values: {
                chkBillDetailsId: chkBillDetailsId,
                un: un,
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                api.hideProgress();

                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                if (myobj.code == '0') {

                    var chkResult = myobj.chkResult;

                    list = eval(myobj.list);

                    $("#citemForm").children().remove();

                    //检测明细检测结果（0未判定，1不合格，5放行，7合格）
                    var checkResultDivStr = "<div class='aui-input-row' style='background-color: #addbef !important;'>";
                    checkResultDivStr += "<div><label class='aui-input-addon'>检测结果</label></div>";
                    checkResultDivStr += "<div>";

                    if (chkResult == "0") {

                        checkResultDivStr += "<input id='radioBox1' class='aui-radio' type='radio' name='chkResult' value='0' checked>";
                        checkResultDivStr += "<div class='aui-radio-name'>未判定</div>";
                        checkResultDivStr += "<input id='radioBox2' class='aui-radio' type='radio' name='chkResult' value='1'>";
                        checkResultDivStr += "<div class='aui-radio-name'>不合格</div>";
                        checkResultDivStr += "<input id='radioBox3' class='aui-radio' type='radio' name='chkResult' value='5'>";
                        checkResultDivStr += "<div class='aui-radio-name'>放行</div>";
                        checkResultDivStr += "<input id='radioBox4' class='aui-radio' type='radio' name='chkResult' value='7'>";
                        checkResultDivStr += "<div class='aui-radio-name'>合格</div>";

                    } else if (chkResult == "1") {

                        checkResultDivStr += "<input id='radioBox1' class='aui-radio' type='radio' name='chkResult' value='0'>";
                        checkResultDivStr += "<div class='aui-radio-name'>未判定</div>";
                        checkResultDivStr += "<input id='radioBox2' class='aui-radio' type='radio' name='chkResult' value='1' checked>";
                        checkResultDivStr += "<div class='aui-radio-name'>不合格</div>";
                        checkResultDivStr += "<input id='radioBox3' class='aui-radio' type='radio' name='chkResult' value='5'>";
                        checkResultDivStr += "<div class='aui-radio-name'>放行</div>";
                        checkResultDivStr += "<input id='radioBox4' class='aui-radio' type='radio' name='chkResult' value='7'>";
                        checkResultDivStr += "<div class='aui-radio-name'>合格</div>";

                    } else if (chkResult == "5") {

                        checkResultDivStr += "<input id='radioBox1' class='aui-radio' type='radio' name='chkResult' value='0'>";
                        checkResultDivStr += "<div class='aui-radio-name'>未判定</div>";
                        checkResultDivStr += "<input id='radioBox2' class='aui-radio' type='radio' name='chkResult' value='1'>";
                        checkResultDivStr += "<div class='aui-radio-name'>不合格</div>";
                        checkResultDivStr += "<input id='radioBox3' class='aui-radio' type='radio' name='chkResult' value='5' checked>";
                        checkResultDivStr += "<div class='aui-radio-name'>放行</div>";
                        checkResultDivStr += "<input id='radioBox4' class='aui-radio' type='radio' name='chkResult' value='7'>";
                        checkResultDivStr += "<div class='aui-radio-name'>合格</div>";

                    } else if (chkResult == "7") {

                        checkResultDivStr += "<input id='radioBox1' class='aui-radio' type='radio' name='chkResult' value='0'>";
                        checkResultDivStr += "<div class='aui-radio-name'>未判定</div>";
                        checkResultDivStr += "<input id='radioBox2' class='aui-radio' type='radio' name='chkResult' value='1'>";
                        checkResultDivStr += "<div class='aui-radio-name'>不合格</div>";
                        checkResultDivStr += "<input id='radioBox3' class='aui-radio' type='radio' name='chkResult' value='5'>";
                        checkResultDivStr += "<div class='aui-radio-name'>放行</div>";
                        checkResultDivStr += "<input id='radioBox4' class='aui-radio' type='radio' name='chkResult' value='7' checked>";
                        checkResultDivStr += "<div class='aui-radio-name'>合格</div>";

                    }
                    checkResultDivStr += "</div>";
                    checkResultDivStr += "</div>";
                    $("#citemForm").append(checkResultDivStr);

                    for (var i = 0; i < list.length; i++) {
                        var controlType = list[i].controlType; //获取控件类型（0手动输入1多选2单选）
                        var citemId = list[i].citemId;
                        var citemCode = list[i].citemCode;
                        var citemName = list[i].citemName;
                        var citemRemark = list[i].citemRemark;
                        var dept = list[i].dept;
                        var cavityNum = list[i].cavityNum;//型腔号

                        var labContent = "";
                        // var labContent = citemCode + "[" + citemName + "]";
                        if (citemRemark != null && citemRemark != "" && citemRemark != undefined) {
                            labContent = "[" + cavityNum + "]" + citemName + "[" + citemRemark + "]";
                        } else {
                            labContent = citemName;
                        }

                        var labContent_new = '';
                        for (let i = 0; i < labContent.length; i += 19) {
                            labContent_new += labContent.substring(i, i + 19) + '<br>';
                        }

                        var itemId = "citem" + i;
                        //alert(controlType);

                        //定量-手动输入
                        if ("0" == list[i].controlType) {

                            var minValue = list[i].minValue;
                            var maxValue = list[i].maxValue;
                            var standardValue = list[i].standardValue;
                            var chkValue = list[i].chkValue;

                            var valContent = "上限值：" + maxValue + " 下限值：" + minValue + " 标准值：" + standardValue;

                            var str = "<div  style='display: flex; justify-content: space-between; width: 100%;'>";
                            str += "<span style='width:85% ;white-space:nowrap;overflow:hidden;text-align: left;color: #000;'>" + labContent_new + "</span>";
                            str += " <span style='width:15%; white-space:nowrap;overflow:hidden;text-overflow:ellipsis;text-align:right;color: #000;'>" + dept +
                                "</span>";
                            // var divStr = "<div class='aui-input-row' style='display: flex; justify-content: space-between; width: 100%;''><span style='width:" + api.frameWidth + "px; white-space:nowrap;overflow:hidden;text-overflow:ellipsis;text-align:left;color: #000;'>" + labContent +
                            //     "</span><span style='width:" + api.frameWidth + "px; white-space:nowrap;overflow:hidden;text-overflow:ellipsis;text-align:right;color: #000;'>" + labContent +
                            //     "</span></div><div class='aui-pull-left' >";
                            str += "</div>";
                            str += "<div>";
                                str += "<div class='aui-pull-left' style='border-bottom: 1px solid #c8c7cc;top: 15px;width:100%'  >";
                            str += "<input type='number' class='aui-input' placeholder='" + valContent + "' style='' id=\"" + itemId + "\" value='" + chkValue + "' onchange='setChkResult(\"" + minValue + "\",\"" + maxValue +
                                "\",this.id)'/>";
                            str += "</div>";
                            str += "</div>";
                            //手动输入
                            $("#citemForm").append(str);

                            //定性-多选
                        } else if ("1" == list[i].controlType) {

                            //多选（checkbox）
                            // var divStr = "<div class='aui-input-row'><p style='width:" + api.frameWidth + "px; white-space:nowrap;overflow:hidden;text-overflow:ellipsis;text-align:left;color: #000;'>" + labContent +
                            //     "</p><div class='aui-pull-left' >";
                            var divStr = "<div   style='display: flex; justify-content: space-between; width: 100%;'><span style='width:85%; white-space:nowrap;overflow:hidden;text-align:left;color: #000;'>" + labContent_new +
                                "</span><span style='width:15%; white-space:nowrap;overflow:hidden;text-overflow:ellipsis;text-align:right;color: #000;'>" + dept +
                                "</span></div><div class='aui-pull-left' style='border-bottom: 1px solid #c8c7cc;top: 15px;width:100%'  >";
                            var citemDs = eval(list[i].citemDs);
                            for (var j = 0; j < citemDs.length; j++) {
                                var citemDId = citemDs[j].id;
                                var citemDCode = citemDs[j].citemDCode;
                                var citemDName = citemDs[j].citemDName;
                                //0待选，1未选，2已选
                                var selectFlag = citemDs[j].selectFlag;
                                //默认选项：1是，2否
                                var isDefault = citemDs[j].isDefault;
                                if (selectFlag == "2") {
                                    divStr = divStr + "<input class='aui-checkbox' type='checkbox' name=\"" + itemId + "\" value=\"" + citemDId + "\" checked><div class='aui-checkbox-name'>" + citemDName + "</div>";
                                } else {
                                    if (isDefault == "2") {
                                        divStr = divStr + "<input class='aui-checkbox' type='checkbox' name=\"" + itemId + "\" value=\"" + citemDId + "\" checked><div class='aui-checkbox-name'>" + citemDName + "</div>";
                                    } else {
                                        divStr = divStr + "<input class='aui-checkbox' type='checkbox' name=\"" + itemId + "\" value=\"" + citemDId + "\" ><div class='aui-checkbox-name'>" + citemDName + "</div>";
                                    }
                                }
                            }

                            $("#citemForm").append(divStr + "</div>");

                            //定性-单选
                        } else {

                            //单选（radio）
                            var divStr = "<div style='display: flex; justify-content: space-between; width: 100%;	'><span style='width:85%; white-space:nowrap;overflow:auto;text-align:left;color: #000;'>" + labContent_new +
                                "</span><span style='width:15%; white-space:nowrap;overflow:hidden;text-overflow:ellipsis;text-align:right;color: #000;'>" + dept +
                                "</span></div><div class='aui-pull-left' style='border-bottom: 1px solid #c8c7cc;top: 15px;width:100%' >";
                            var citemDs = eval(list[i].citemDs);
                            for (var j = 0; j < citemDs.length; j++) {
                                var citemDId = citemDs[j].id;
                                var citemDCode = citemDs[j].citemDCode;
                                var citemDName = citemDs[j].citemDName;
                                //0待选，1未选，2已选
                                var selectFlag = citemDs[j].selectFlag;
                                //默认选项：1是，2否
                                var isDefault = citemDs[j].isDefault;
                                if (selectFlag == "2") {
                                    console.log(1)
                                    divStr = divStr + "<input class='aui-radio'  type='radio' name=\"" + itemId + "\" value=\"" + citemDId + "\" checked><div class='aui-radio-name'>" + citemDName + "</div>";
                                } else {
                                    if (isDefault == "2") {
                                        console.log(2)
                                        divStr = divStr + "<input class='aui-radio'   type='radio' name=\"" + itemId + "\" value=\"" + citemDId + "\" checked><div class='aui-radio-name'>" + citemDName + "</div>";
                                    } else {
                                        console.log(3)
                                        divStr = divStr + "<input class='aui-radio'   type='radio' name=\"" + itemId + "\" value=\"" + citemDId + "\"><div class='aui-radio-name'>" + citemDName + "</div>";
                                    }
                                }
                            }

                            $("#citemForm").append(divStr + "</div>");
                        }
                    }
                    $("#citemForm").append("<br /><div class='aui-btn aui-btn-info aui-btn-block' style='margin-top:25px' onclick='doSave()'>保存</div>");

                    //失败
                } else {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }

    //比较检测值
    function setChkResult(minValue, maxValue, id) {
        // var itemValue = document.getElementById(id).value;
        // //alert(itemValue);
        //
        // if (parseFloat(itemValue) < parseFloat(minValue) || parseFloat(itemValue) > parseFloat(maxValue)) {
        //     //alert("不合格");
        //     document.getElementById('radioBox2').checked = true;
        // } else {
        //     document.getElementById('radioBox1').checked = true;
        // }
    }

    function toCheckIsUnPass(itemValueList, chkResultStr, josnList) {
        console.log(JSON.stringify(itemValueList));
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/toCheckIsUnPass";
        var data = {
            values: {
                list: JSON.stringify(itemValueList),
                un: un,
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    if (myobj.flag == '不合格') {
                        //弹框提示
                        api.confirm({
                            title: '提示',
                            msg: '检测项中存在不合格的检测项，确定放行吗？',
                            buttons: ['确定', '取消']
                        }, function (ret, err) {
                            var index = ret.buttonIndex;
                            if (index == 1) {
                                myFlag = true

                            } else {
                                myFlag = false
                            }
                            doSaveFinally(chkResultStr, josnList)
                        });
                    } else {
                        myFlag = true
                        doSaveFinally(chkResultStr, josnList)
                    }
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //保存
    function doSave() {
        var josnList = [];
        var itemValueList = [];
        for (var i = 0; i < list.length; i++) {

            //获取控件类型（0手动输入1多选2单选）
            var controlType = list[i].controlType;
            var itemId = "citem" + i;
            var itemValue = "";

            //手动输入
            if ("0" == list[i].controlType) {

                itemValue = document.getElementById(itemId).value;

                //多选
            } else if ("1" == list[i].controlType) {

                var checkBoxObj = document.getElementsByName(itemId); //这个是以标签的name来取控件
                for (m = 0; m < checkBoxObj.length; m++) {
                    if (checkBoxObj[m].checked) {
                        itemValue = itemValue + checkBoxObj[m].value + ",";
                    }
                }
                if (checkBoxObj.length > 0) {
                    //有选中，则最后一个需要去掉“,”
                    itemValue = itemValue.substring(0, itemValue.length - 1);
                }

                //单选
            } else {
                var radioObj = document.getElementsByName(itemId); //这个是以标签的name来取控件
                for (n = 0; n < radioObj.length; n++) {
                    if (radioObj[n].checked) {
                        itemValue = radioObj[n].value;
                        itemValueList.push({
                            "sysid": itemValue
                        })
                    }
                }
            }

            josnList.push({
                "id": list[i].id,
                "chkValue": itemValue
            });
        }

        // console.log(JSON.stringify(josnList));

        var chkResultObj = document.getElementsByName("chkResult"); //这个是以标签的name来取控件
        var chkResultStr = ""; //检测结果（必填）
        for (j = 0; j < chkResultObj.length; j++) {
            if (chkResultObj[j].checked) {
                chkResultStr = chkResultObj[j].value;
            }
        }

        // console.log(chkResultStr);

        if (chkResultStr == "") {
            api.alert({
                title: '提示',
                msg: '请选择检测结果',
            }, function (ret, err) {

            });
            return;
        }

        if (chkResultStr == '5') {

            let containsCheckResultOne = false;
            josnList.forEach(item => {
                if (item.chkValue == "" || item.chkValue == null || item.chkValue == undefined) {
                    containsCheckResultOne = true;
                }
            });

            console.log(containsCheckResultOne)
            //说明存在异常的数据
            if (containsCheckResultOne) {
                api.toast({
                    msg: '存在未处理的检测项,请处理后再进行保存！',
                    duration: 3000
                });
                josnList = []
                return;
            }
            //检查是否存在不合格的检测项
            toCheckIsUnPass(itemValueList, chkResultStr, josnList)
        } else {
            myFlag = true
            doSaveFinally(chkResultStr, josnList)
        }
    }

    function doSaveFinally(chkResultStr, josnList) {
        if (myFlag) {
            console.log('检查项中存在不合格的检测项，进行放行操作')
        } else {
            console.log('检查项中存在不合格的检测项，进行取消操作')
            return
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/saveUqcmChkBillDetail";
        var data = {
            values: {
                chkBillDetailsId: chkBillDetailsId, //检测单明细id
                chkResult: chkResultStr, //检测单明细检测项结果
                josnList: JSON.stringify(josnList),
                un: un,
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {

                    //返回主界面，刷新表格数据
                    api.closeWin();
                    var name = api.pageParam.name;
                    api.execScript({
                        name: name,
                        script: 'doSearchChkBillDetails()'
                    });

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>